package _002_liang_shu_xiang_jia;

/**
 * App
 *
 * @author yuand
 * @date 2021/8/24
 **/
public class App {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


    public static void main(String[] args) {

    }

    static class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode root = new ListNode();
            ListNode prev = root;
            int jw = 0;
            while (l1 != null || l2 != null || jw != 0) {
                int value = jw;
                if (l1 != null) value += l1.val;
                if (l2 != null) value += l2.val;
                ListNode current = new ListNode();
                if (value > 9) {
                    current.val = value - 10;
                    jw = 1;
                } else {
                    current.val = value;
                    jw = 0;
                }
                prev.next = current;
                prev = prev.next;
                if (l1 != null) l1 = l1.next;
                if (l2 != null) l2 = l2.next;
            }
            return root.next;
        }
    }
}
